package com.github.pig.sales.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.common.util.Query;
import com.github.pig.sales.service.ISalesPublicRepertoryManageService;
import com.github.pig.training.entity.TrainingStudent;
import com.github.pig.training.mapper.TrainingStudentMapper;
import com.github.pig.training.service.ITrainingStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SalesPublicRepertoryManageServiceImpl extends ServiceImpl<TrainingStudentMapper ,TrainingStudent> implements ISalesPublicRepertoryManageService {

    @Autowired
    private TrainingStudentMapper trainingStudentMapper;
    @Autowired
    private ITrainingStudentService studentService;

    @Override
    public Page getList(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        EntityWrapper ew = new EntityWrapper<>(new TrainingStudent());
        ew.where("follow_up_status='1602'");
        if(params.get("beginDate")!=null){
            String date = params.get("beginDate").toString();
            date = date.substring(0,date.indexOf("T"));
            ew.and("create_time>'"+date+"' ");
            Calendar c = Calendar.getInstance();
            try { c.setTime(sdf.parse(params.get("endDate").toString())); } catch (ParseException e){}
            c.add(Calendar.DAY_OF_MONTH, 1);
            ew.and("create_time<'"+sdf.format(c.getTime())+"'");
            params.remove("beginDate");
            params.remove("endDate");
        }
        Map<String,Object> map1 = com.github.pig.training.util.Query.getEntityWrapperByCondition(ew,params);
        ew = (EntityWrapper) map1.get("entityWrapper");
        params = (Map<String,Object>) map1.get("map");
        if(params.get("number")!=null&&Integer.valueOf(params.get("number").toString())!=0){
            Query query = new Query<>(params);
            int number = Integer.valueOf(params.get("number").toString());
            String limit = ew.toString().substring(ew.toString().indexOf(":") + 4,ew.toString().length());
            Page page = new Page(query.getCurrent()+1,query.getSize());
            List<TrainingStudent> list = trainingStudentMapper.getStudentByFlowupFrequency(number,limit,
                    (query.getCurrent()-1)*query.getSize(),query.getSize());
            page.setRecords(list);
            return page;
        }else{
            params.remove("number");
            return studentService.selectPage(new Query<>(params),ew);
        }
    }
}
